﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyThuoc.BUS;
using System.Globalization;
namespace QuanLyThuoc.PresentionLayer
{
    public partial class frmThuocXuat : Form
    {
        public int keyValue = 0;
        int vitri = 0;
        bool plAddMode = false;
        bool plEditMode = false;
        BindingSource dsThuocXuat;
        private AutoCompleteStringCollection auBN = new AutoCompleteStringCollection();
        private AutoCompleteStringCollection auTenThuoc = new AutoCompleteStringCollection();
        private AutoCompleteStringCollection auphieunhap = new AutoCompleteStringCollection();
        private AutoCompleteStringCollection autoathuoc = new AutoCompleteStringCollection();
        public frmThuocXuat()
        {
            InitializeComponent();
        }

        private void frmThuocXuat_Load(object sender, EventArgs e)
        {
            this.LoadTenBenhNhan();
            this.LoadTenThuocTheoID();
            this.HienThiDanhSach();
            this.LoadPhieuNhap();
        }
        private void HienThiDanhSach()
        {
            dsThuocXuat = new BindingSource();
            dsThuocXuat.DataSource = ThuocXuatBUS.SelectAll_ThuocXuat();
            grdList.AutoGenerateColumns = false;
            grdList.DataSource = dsThuocXuat;

        }
        private void LoadTenThuocTheoID()
        {
            this.auTenThuoc.Clear();
            DataTable dtthuoc = ThuocBUS.LayTenThuocTheoID();
            string lcValue = "";
            foreach (DataRow row in dtthuoc.Rows)
            {

                lcValue = row["TenThuoc"].ToString().PadRight(100, ' ') + "|" + row["IDThuoc"].ToString().Trim();
                //this.auTenNCC.Add(row["TenNCC"].ToString());
                this.auTenThuoc.Add(lcValue);
            }
            this.txtTenThuoc.AutoCompleteSource = AutoCompleteSource.CustomSource;
            this.txtTenThuoc.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.txtTenThuoc.AutoCompleteCustomSource = this.auTenThuoc;
        }
        private void LoadTenBenhNhan()
        {
            this.auBN.Clear();
            DataTable dtBN = BenhNhanBUS.LayIDBenhNhanTheoTen();
            string lcValue = "";
            foreach (DataRow row in dtBN.Rows)
            {

                lcValue = row["TenBN"].ToString().PadRight(100, ' ') + "|" + row["IDBN"].ToString().Trim();
                //this.auTenNCC.Add(row["TenNCC"].ToString());
                this.auBN.Add(lcValue);
            }
            this.txtTenBN.AutoCompleteSource = AutoCompleteSource.CustomSource;
            this.txtTenBN.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.txtTenBN.AutoCompleteCustomSource = this.auBN;
        }
        private void LoadPhieuNhap()
        {
            this.auphieunhap.Clear();
            DataTable dtPN = PhieuNhapBUS.LayIDSoPhieuNhap();
            string lcValue = "";
            foreach (DataRow row in dtPN.Rows)
            {

                lcValue =  row["IDPN"].ToString().Trim();
                
                this.auBN.Add(lcValue);
            }
            this.txtSoPhieu.AutoCompleteSource = AutoCompleteSource.CustomSource;
            this.txtSoPhieu.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.txtSoPhieu.AutoCompleteCustomSource = this.auphieunhap;
 
        }
        private void LoadToaThuoc()
        {
            this.autoathuoc.Clear();
            DataTable dtToa= ToaThuocBUS.LayIDToaThuoc();
            string lcValue = "";
            foreach (DataRow row in dtToa.Rows)
            {

                lcValue = row["IDToa"].ToString().Trim() + "|" + row["IDBN"].ToString().Trim() + "|" + row["TenBN"].ToString().Trim().PadLeft(100,' ');

                this.auBN.Add(lcValue);
            }
            this.txtIDToa.AutoCompleteSource = AutoCompleteSource.CustomSource;
            this.txtIDToa.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.txtIDToa.AutoCompleteCustomSource = this.autoathuoc;
        }
        private void GanDK(DataGridViewRow r)
        {
            try
            {
                if (grdList.Rows.Count != 0)
                {
                    this.txtIDCT.Text = r.Cells[0].Value.ToString();
                    this.txtSoPhieu.Text = r.Cells[1].Value.ToString();
                    this.txtIDToa.Text = r.Cells[2].Value.ToString();
                    this.txtIDBN.Text = r.Cells[3].Value.ToString();
                    this.txtTenBN.Text = r.Cells[4].Value.ToString();
                    this.txtIDThuoc.Text = r.Cells[5].Value.ToString();
                    this.txtTenThuoc.Text = r.Cells[6].Value.ToString();
                    this.txtDVT.Text = r.Cells[7].Value.ToString();

                    this.txtDuongDung.Text = r.Cells[8].Value.ToString();
                    this.txtNgaySX.Text = r.Cells[9].Value.ToString();
                    this.txtHanSD.Text = r.Cells[10].Value.ToString();
                    this.txtSL_S.Text = r.Cells[11].Value.ToString();
                    this.txtSL_Trua.Text = r.Cells[12].Value.ToString();
                    this.txtSL_C.Text = r.Cells[13].Value.ToString();
                    this.txtSL1Ngay.Text = r.Cells[14].Value.ToString();

                    this.txtSoNgayuong.Text = r.Cells[15].Value.ToString();
                    this.txtTongSL.Text = r.Cells[16].Value.ToString();
                    this.txtSLX.Text = r.Cells[17].Value.ToString();
                    this.txtGiaban.Text = r.Cells[18].Value.ToString();
                    this.txtThanhTien.Text = r.Cells[19].Value.ToString();
                    this.txtGhiChu.Text = r.Cells[20].Value.ToString();
                    this.txtIDCT.Text = r.Cells[21].Value.ToString();
                }
                else
                {
                    this.txtSoPhieu.Text = "";
                    this.txtIDToa.Text = "";
                    this.txtIDBN.Text = "";
                    this.txtTenBN.Text ="";
                    this.txtIDThuoc.Text = "";
                    this.txtTenThuoc.Text = "";
                    this.txtDVT.Text = "";

                    this.txtDuongDung.Text = "";
                    this.txtNgaySX.Text = "";
                    this.txtHanSD.Text = "";
                    this.txtSL_S.Text = "";
                    this.txtSL_Trua.Text = "";
                    this.txtSL_C.Text = "";
                    this.txtSL1Ngay.Text = "";

                    this.txtSoNgayuong.Text = "";
                    this.txtTongSL.Text = "";
                    this.txtSLX.Text = "";
                    this.txtGiaban.Text = "";
                    this.txtThanhTien.Text = "";
                    this.txtGhiChu.Text = "";
                    this.txtIDCT.Text = "";
                }
            }
            catch (Exception ex)
            {

            }
        }
        private void AddBlank()
        {
                     this.txtSoPhieu.Text = "";
                    this.txtIDToa.Text = "";
                    this.txtIDBN.Text = "";
                    this.txtTenBN.Text ="";
                    this.txtIDThuoc.Text = "";
                    this.txtTenThuoc.Text = "";
                    this.txtDVT.Text = "";

                    this.txtDuongDung.Text = "";
                    this.txtNgaySX.Text = "";
                    this.txtHanSD.Text = "";
                    this.txtSL_S.Text = "";
                    this.txtSL_Trua.Text = "";
                    this.txtSL_C.Text = "";
                    this.txtSL1Ngay.Text = "";

                    this.txtSoNgayuong.Text = "";
                    this.txtTongSL.Text = "";
                    this.txtSLX.Text = "";
                    this.txtGiaban.Text = "";
                    this.txtThanhTien.Text = "";
                    this.txtGhiChu.Text = "";
        }
        private void ResetControl()
        {
            this.txtIDCT.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtSoPhieu.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtTenThuoc.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtIDToa.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtIDBN.ReadOnly = !(this.plAddMode || this.plEditMode);
            
            this.txtGiaban.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtGhiChu.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtDVT.ReadOnly = !(this.plAddMode || this.plEditMode);

            this.txtDuongDung.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtIDThuoc.ReadOnly = !(this.plAddMode || this.plEditMode);
            
            this.txtSL_C.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtSL_S.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtSL_Trua.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtSL1Ngay.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtSLX.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtSoNgayuong.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtTongSL.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtThanhTien.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.cmdAdd.Image = (this.plAddMode || this.plEditMode) ? QuanLyThuoc.Properties.Resources.save24 : QuanLyThuoc.Properties.Resources.Add24;
            this.cmdEdit.Image = (this.plAddMode || this.plEditMode) ? QuanLyThuoc.Properties.Resources.undo24 : QuanLyThuoc.Properties.Resources.Edit24;

            this.cmdAdd.Text = (this.plAddMode || this.plEditMode) ? "Lưu [F2]" : "Thêm [F2]";
            this.cmdEdit.Text = (this.plAddMode || this.plEditMode) ? "Hủy [F4]" : "Sửa [F4]";

            this.grdList.Enabled = !this.plAddMode;

        }
        public void Gan_Click()
        {
            try
            {
                if (this.grdList.SelectedRows.Count == 0)
                    return;
                DataGridViewRow rs = this.grdList.SelectedRows[0];
                GanDK(rs);
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi trong quá trình thực hiện");

            }
        }
        private void AddNew()
        {
            this.plAddMode = true;
            this.plEditMode = false;


        }
        private void Edit()
        {
            this.plEditMode = true;
            this.plAddMode = false;
        }
        private void Undo()
        {
            //this.bmbList.CancelCurrentEdit();
            vitri = grdList.CurrentCell.RowIndex;
            this.plAddMode = false;
            this.plEditMode = false;
        }
        private void Find()
        {
            if (txtFind.Text.Trim() == "")
            {
                this.HienThiDanhSach();
            }
            else
            {
                //dsthuoc.DataSource = ThuocBUS.SearchThuocByCondition(txtFind.Text);
                //grdList.DataSource = dsthuoc;
            }
        }
        private void Save()
        {


            string tenthuoc = txtTenThuoc.Text;
            int mabn = int.Parse(txtIDBN.Text);
            string tenbn = txtTenBN.Text;
            int mathuoc = int.Parse(txtIDThuoc.Text);
            string matoa = txtIDToa.Text;
            string sl_sang = txtSL_S.Text;
            string dvt = txtDVT.Text;
            decimal sl1ngay = Convert.ToDecimal(txtSL1Ngay.Text);

            string sl_trua = txtSL_Trua.Text;
            string sl_chieu = txtSL_C.Text;
            string duongdung = txtDuongDung.Text;
            string ghichu = txtGhiChu.Text;
            int songayuong = int.Parse(txtSoNgayuong.Text);
            int tongsl = int.Parse(txtTongSL.Text);
            int slXuat = int.Parse(txtSLX.Text);
            int giaban = int.Parse(txtGiaban.Text);
            int thanhtien = int.Parse(txtThanhTien.Text);
            DateTime ngaysx = Convert.ToDateTime(txtNgaySX.Text);
            DateTime hansd = Convert.ToDateTime(txtHanSD.Text);
            string idpn = txtSoPhieu.Text;
            if (string.IsNullOrEmpty(this.txtIDCT.Text.Trim()))
            {
                ThuocXuatBUS.InsertThuocXuat(mabn, matoa, mathuoc, tenthuoc, dvt, sl_sang, sl_trua, sl_chieu, sl1ngay, songayuong, tongsl, slXuat, giaban, thanhtien, duongdung, idpn, ngaysx, hansd, ghichu);
            }
            else
            {
                int mact = int.Parse(txtIDCT.Text);

                ThuocXuatBUS.UpdateThuocXuat(mact,mabn, matoa, mathuoc, tenthuoc, dvt, sl_sang, sl_trua, sl_chieu, sl1ngay, songayuong, tongsl, slXuat, giaban, thanhtien, duongdung, idpn, ngaysx, hansd, ghichu);
            }

        }
        private void Delete()
        {
            try
            {
                int mact = int.Parse(txtIDCT.Text.ToString());

                if (MessageBox.Show("Bạn có muốn xóa phiếu thuốc xuất '" + txtIDCT.Text + "'không ??", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (vitri == grdList.RowCount)
                    {
                        ThuocXuatBUS.Delete_ThuocXuat(mact);
                        this.AddBlank();
                        grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                        grdList.Rows[vitri - 1].Selected = true;
                        Gan_Click();
                    }
                    if (vitri == 0)
                    {
                        ThuocXuatBUS.Delete_ThuocXuat(mact);
                        Gan_Click();
                        grdList.CurrentCell = grdList.Rows[vitri + 1].Cells[0];
                        grdList.Rows[vitri + 1].Selected = true;
                        this.AddBlank();
                        Gan_Click();
                    }
                    if (vitri > 0 && vitri < grdList.RowCount)
                    {
                        ThuocXuatBUS.Delete_ThuocXuat(mact);
                        this.AddBlank();
                        grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                        grdList.Rows[vitri - 1].Selected = true;
                        Gan_Click();
                    }

                }
            }
            catch (Exception ex)
            {
                // this.AddBlank();
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData.Equals(Keys.Escape))
            {
                //this.keyValue = "";
                if (!(this.plAddMode || this.plEditMode))
                {
                    this.Close();
                }
                else
                {
                    DialogResult dr = MessageBox.Show("Bạn có muốn đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                        this.Close();
                }
            }
            else if (keyData.Equals(Keys.F2))
            {
                if ((this.plAddMode || this.plEditMode))
                    this.Save();
                else
                    this.AddNew();

                this.ResetControl();
            }
            else if (keyData.Equals(Keys.F3))
            {
                if (!(this.plAddMode || this.plEditMode))
                {
                    if (string.IsNullOrEmpty(this.txtFind.Text.Trim()))
                    {
                        this.txtFind.Select();
                        this.txtFind.SelectAll();
                    }
                    else
                    {
                        this.Find();
                    }
                }
            }
            else if (keyData.Equals(Keys.F4))
            {
                if ((this.plAddMode || this.plEditMode))
                    this.Undo();
                else
                    this.Edit();
                this.ResetControl();
            }

            else if (keyData.Equals(Keys.F7))
            {
                if (!(this.plAddMode || this.plEditMode))
                    this.Delete();
                this.ResetControl();
            }

            return base.ProcessDialogKey(keyData);
        }

        private void grdList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (this.grdList.SelectedRows.Count == 0)
                    return;
                DataGridViewRow rs = this.grdList.SelectedRows[0];
                GanDK(rs);
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi trong quá trình thực hiện");

            }
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (this.plAddMode || this.plEditMode)
                this.Save();
            else
                this.AddBlank();

            this.plAddMode = !this.plAddMode;
            this.HienThiDanhSach();
            this.ResetControl();
        }

        private void cmdEdit_Click(object sender, EventArgs e)
        {
            if (this.plAddMode || this.plEditMode)
            {
                this.Undo();
                this.HienThiDanhSach();
            }
            else
            {

                this.Edit();
            }
            this.HienThiDanhSach();
            this.ResetControl();
        }

        private void cmdDel_Click(object sender, EventArgs e)
        {
            this.Delete();
            HienThiDanhSach();
        }

        private void txtSoPhieu_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtSoPhieu_Leave(object sender, EventArgs e)
        {
            string[] arr = this.txtSoPhieu.Text.Trim().Split('|');
            this.txtSoPhieu.Text = arr[0].Trim();
            
        }

        private void txtTenBN_Leave(object sender, EventArgs e)
        {
            string[] arr = this.txtTenBN.Text.Trim().Split('|');
            this.txtTenBN.Text = arr[0].Trim();
            if (arr.Length > 1)
                this.txtIDBN.Text = arr[1].Trim();
        }

        private void txtIDToa_Leave(object sender, EventArgs e)
        {
            string[] arr = this.txtIDToa.Text.Trim().Split('|');
            this.txtIDToa.Text = arr[0].Trim();
            if (arr.Length > 1)
                this.txtIDBN.Text = arr[1].Trim();
            if (arr.Length > 1)
                this.txtTenBN.Text = arr[1].Trim();
        }
       

        private void txtTenThuoc_Leave(object sender, EventArgs e)
        {
            string[] arr = this.txtTenThuoc.Text.Trim().Split('|');
            this.txtTenThuoc.Text = arr[0].Trim();
            if (arr.Length > 1)
                this.txtIDThuoc.Text = arr[1].Trim();
            //if (arr.Length > 1)
            //    this.txtIDThuoc.Text = arr[1].Trim();
            //if (arr.Length > 1)
            //    this.txtIDThuoc.Text = arr[1].Trim();
        }
    }
}